//
//  SVQRScannerViewController.m
//  ShortVideo-ccinon
//
//  Created by LIN TONG on 2017/6/21.
//  Copyright © 2017年 LIN TONG. All rights reserved.
//

#import "SVQRScannerViewController.h"
#import "SVNavigationBar.h"
#import "SVConfig.h"
#import <Photos/Photos.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import <MobileCoreServices/MobileCoreServices.h>

@interface SVQRScannerViewController ()<UIImagePickerControllerDelegate>

@end

@implementation SVQRScannerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    SVNavigationBar *naviBar = [[SVNavigationBar alloc]init];
    naviBar.showBackButton = YES;
    [naviBar setBackHander:^{
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
    }];
    [naviBar setTitle:@"扫描二维码"];
    [naviBar setHidenAccountButton:YES];
    [self.view addSubview:naviBar];
    
    UIButton *photosButton = [[UIButton alloc]initWithFrame:CGRectMake(__SCREEN_WIDTH__-S(10)-S(60), 20+7, S(60), 30)];
    photosButton.layer.cornerRadius = S(5);
    photosButton.backgroundColor = UIColorMake(23, 106, 241, 1);
    photosButton.layer.masksToBounds = YES;
    [photosButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    photosButton.titleLabel.font = Font(13);
    [photosButton setTitle:@"相册选取" forState:UIControlStateNormal];
    [photosButton addTarget:self action:@selector(selelctImageFormPhotos:) forControlEvents:UIControlEventTouchUpInside];
    [naviBar addSubview:photosButton];
}

- (void)selelctImageFormPhotos:(UIButton *)sender{
    UIImagePickerController *photoPicker = [[UIImagePickerController alloc] init];
    photoPicker.delegate = self;
    photoPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    photoPicker.mediaTypes = @[(NSString *)kUTTypeImage];
    photoPicker.allowsEditing = YES;
    [self presentViewController:photoPicker animated:YES completion:nil];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.qRScanView.frame = CGRectMake(0, 64, __SCREEN_WIDTH__, __SCREEN_HEIGHT__-64);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)scanResultWithArray:(NSArray<LBXScanResult*>*)array{
    
    for (int i=0;i<array.count;i++){
        LBXScanResult *result = array[i];
        NSString *str = result.strScanned;
        if(str.length > 0){
            if(self.completionHander){
                [self dismissViewControllerAnimated:YES completion:nil];
                self.completionHander(YES, str);
                return;
            }
        }
    }
    
    if(self.completionHander){
        self.completionHander(NO, nil);
    }
}

- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

- (void)detectImage:(UIImage *)image{
    NSData *imageData = UIImagePNGRepresentation(image);
    CIImage *ciImage = [CIImage imageWithData:imageData];
    
    //创建探测器
    CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy: CIDetectorAccuracyLow}];
    NSArray *feature = [detector featuresInImage:ciImage];
    
    //取出探测到的数据
    for (CIQRCodeFeature *result in feature) {
        [self dismissViewControllerAnimated:YES completion:nil];
        NSString *url = result.messageString;
        self.completionHander(YES, url);
        break;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - UIImageViewController delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo{
    
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    [picker dismissViewControllerAnimated:YES completion:nil];
    if([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString*)kUTTypeImage]){
        UIImage *image = [info objectForKey: UIImagePickerControllerEditedImage];
        [self detectImage:image];
    }
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

@end
